/*
 * JDBCUtil.java
 *
 * Created on July 17, 2007, 12:16 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.usp.poli.mfc.struts.DAO.JDBC;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author win
 */
public abstract class JDBCUtil { // Nao precisa instanciar essa classe, soh metodos static
    private static DataSource ds = null;
    /* BLOCO DE INICIALIZACAO ESTATICO
     * NAO MEXER SOB HIPOTESE ALGUMA. DAH PAU NO RESTO
     */
    static {
        try {
            Context initCtx = new InitialContext(); // http://216.239.51.104/search?q=cache:0vZeloRx6ZAJ:tomcat.apache.org/tomcat-5.0-doc/jndi-resources-howto.html+java:comp/env+tomcat&hl=en&ct=clnk&cd=1&client=firefox-a
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            ds = (DataSource) envCtx.lookup("jdbc/mfctable");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
    
    /* UTILIZAR SEMPRE ESSA FUNCAO PARA OBTER A CONEXAO COM O BANCO DE DADOS
     * NAO UTILIZAR NENHUM OUTRO LUGAR
     */
    public static Connection getConnection() throws SQLException{
        return ds.getConnection();
    }
}
